<?php
/**********************
**reconfiguredevice.php
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once('private/dba.inc');
require_once('private/device.inc');
require_once('private/top.inc');
require_once('private/bottom.inc');

//TODO: replace this with values from DeviceTypes class
function getDeviceTypes(&$types)
{
	$types = array(array(1, "Television"), array(2, "Lamp"), array(3, "Stereo"), array(4, "DVD player"));
	return TT_OK;
}

function processReconfigureRequest()
{
	//Did we get a valid reconfigure request?	
	if( !isset($_POST['deviceId']) || !isset($_POST['deviceType']) || !isset($_POST['deviceName']) )
	{
		//No error, nothing to process
		return TT_OK;
	}
	$id = $_POST['deviceId'];	
	$type = $_POST['deviceType'];
	$name = $_POST['deviceName'];
		
	if( $id == '' || $type == '' || $name == '' )
	{
		echo "Reconfigure failed: please select a type and enter a name\n";
	}
	else
	{
		$device = new Device($id);
		if( TT_FAILED($device->setType($type)) || TT_FAILED($device->setName($name)) )
		{
			echo "There was a problem reconfiguring the device: " . Device::getLastError();
		}
		else
		{
			echo "Device was reconfigured successfully!";
		}
	}

	echo "
		<BR>
		<BR>
		";
	return TT_OK;
}

function selectionForm($idIn)
{
	/***********
	Selection form
	***********/
	//We track a string so that if we can't generate the form, we don't get odd results on the screen
	//Begin form
	$formStr = "<FORM name=\"input\" action=\"reconfiguredevice.php\" method=\"POST\">";
	
	//Device name select box
	$formStr .= "
		Reconfigure device:  
		<SELECT name=\"deviceId\">
		";
	$rc = Device::getDevices($devices);
	if( TT_FAILED($rc) )
	{
		echo "Failed to get devices!<BR>";
		return TT_GENERAL;
	}
	/*
	else if( sizeof($devices) == 0 )
	{
		echo "No devices!<BR>";
	}
	*/
	//Make a select entry for each device
	foreach( $devices as $key => $value )
	{
		$id = $value;
		$device = new Device($id);
		if( TT_FAILED($device->getName($name)) )
		{
			echo "Couldn't get name<BR>";
			return TT_GENERAL;
		}
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $id . "\"";
		//Make sure last default is selected
		if( $id == $idIn )
		{
			$formStr .= " selected";  
		}
	 	$formStr .= ">" . $name;
		$formStr .= "</OPTION>\n";
	}
	$formStr .= "</SELECT>";
	
	//Space between input data and action buttons
	/*$formStr .= "
		<BR />
		<BR />
		<BR />
		";
	*/
	//Submit button	
	$formStr .= "
		&nbsp;&nbsp;&nbsp;&nbsp;
		<INPUT name=\"Submit\" type=\"submit\" value=\"Select for reconfiguration\" />
		<BR />
		<BR />";
	
	//And end the form
	$formStr .= "</FORM>";

	//We didn't blow up, display the form
	echo $formStr;
	return TT_OK;
}

function reconfigureForm($id)
{
	/***********
	Reconfigure form
	***********/
	if( $id < 0 )
	{
		echo "No device selected<BR>";
		return TT_GENERAL;
	}
	//state should be queried
	
	$device = new Device($id);
	if( TT_FAILED($device->getType($type)) || TT_FAILED($device->getName($name)) )
	{
		echo "There was a problem getting device info: " . Device::getLastError();
		return TT_GENERAL;
	}

	//Begin form
	$formStr = "<FORM name=\"input\" action=\"reconfiguredevice.php\" method=\"POST\">";

	//Id
	$formStr .= "<INPUT name=\"deviceId\" type=\"hidden\" value=\"$id\"/>";

	//Name
	$formStr .= "Device Name:  <INPUT name=\"deviceName\" type=\"text\" value=\"$name\" size=\"20\" maxlength=\"64\" />";

	$formStr .=  "
		<BR />
		<BR />
		";
	
	//Device types select box
	$formStr .= "Type of Device:  <select name=\"deviceType\">";
	$rc = Device::getDeviceTypes($deviceTypes);
	if( TT_FAILED($rc) )
	{
		echo "Could not get device types<BR />";
		return TT_GENERAL;
	}
	foreach( $deviceTypes as $key => $value )
	{
		//User wants string values, but we want the IDs
	 	$formStr .= "<OPTION value=\"". $value[0] . "\">" . $value[1] . "</OPTION>";
	}
	$formStr .= "</SELECT>";
	
	$formStr .=  "
		&nbsp;&nbsp;&nbsp;&nbsp;
		<A href=\"editdevicetypes.php\">edit device types</a>
		<BR />
		<BR />
		<BR />
		<BR />
		";
	
	$formStr .=  "
		<INPUT name=\"Submit\" type=\"submit\" value=\"Submit\" />
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		<A href=\"main.php\">Cancel</A>
		</FORM>
		";
	
	echo $formStr;
}

function display()
{
	top("Reconfigure A Device");
	echo "
		<HTML>
		<BODY>
		";

	//This will add its own trailing whitespace as needed
	processReconfigureRequest();

	//A new device to display?
	if( isset($_POST['deviceId']) )
	{
		$id = $_POST['deviceId'];
	}
	else
	{
		//First time on page
		//Get a default device, how about the last added
		//User is most likely to make a mistake on the last thing they added
		$rc = Device::getDevices($devices);
		if( TT_FAILED($rc) || sizeof($devices) == 0 )
		{
			$id = -1;
		}
		else
		{
			//Get the last element
			$id = $devices[sizeof($devices) - 1];
		}
	}
	
	selectionForm($id);

	echo "
		<BR />
		<BR />
		";

	reconfigureForm($id);	
	
	echo "
		</BODY>\n
		</HTML>
		";
	bottom();
}

display();

?>
